*****************************************************************
*****************************************************************
*****                                                       *****
*****       Mona Morgan-Collins (King's College London)     *****
*****        Contact: mona.morgan-collins@kcl.ac.uk   m     *****
*****                                                       *****
*****               Bringing in the New Votes:              *****
*****       Turnout of Women after Enfranchisement          *****
*****                                                       *****
*****        American Political Science Review              *****
*****                                                       *****
*****           Building replication data sets              *****
*****                                                       *****
*****************************************************************
*****************************************************************

*Stata version 17.0
cd "./vt_repl" /*set your working directory*/ 

*install the following packages
ssc install coefplot, replace
ssc install boottest, replace

*set matsize for Stata/MP and State/SE
set matsize 11000 , permanently 


*This file builds all data sets
	*nor0918.dta replicates Figures 2a; 3; a4a,b; a10b,c; a11b,c; Tables a1; a2; a4; a5; a17
	*nor0918_red.dta replicates Figure 5a; Table a7
	*nor2127.dta replicates Figures 2:4; a4c; a9b,c,e; a10f; a11f; Tables a1; a3; a6; a17
	*nor2127_red.dta replicates Figure 5b; Table a7
	*nor_coxfivasmith.dta replicates Figures 6; 7; a5; Tables a12; a13; a14
	
	*norovert.dta replicates Figure a3
	*nor0906 replicates Figure a8
	*nor2127_district replicates Table a10
	*nor09_munic replicates Figure a11
	*nor15_munic replicates Figures a8; a11
	*nor18_munic replicates Figure a9a,d
	*nor09_ad.dta, nor15_ad.dta replicates Figure a7

    *nz.dta replicates analyses using election data from New Zealand 1905, Figures 8a; a10a; a11a; Tables a17; a18
	*swe.dta replicates analyses using election data from Sweden 1920-1921, Figures 8b; a10d; a11d; a9; Tables a17; a18
	*aus.dta replicated analyses using election data from Austria 1927, Figure 8c; a10e; a11e; Tables a17; a18
	
	*tingsten.dta replicates Figure A1
	
*Note: the commands need to be run consecutively to ensure that data sets generated in earlier commands can be used for later commands
	
		
*****************************************************
*****************************************************
**              CREATING NOR09.DTA                 **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor09") firstrow case(lower) clear

replace votes1oth = votes1oth+ votes1kir + votes1t + votes1spr                  //adding  small parties (less than 1.5%) to `other' category"
drop  votes1spr votes1kir votes1t
generate votes1hfv = votes1h +votes1h_fv + votes1fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1909
drop votes1h votes1h_fv  votes1fv 
replace votes2oth = votes2oth+ votes2kir + votes2t + votes2spr                 
drop  votes2spr votes2kir votes2t
generate votes2hfv = votes2h +votes2h_fv + votes2fv                            
drop votes2h votes2h_fv  votes2fv 

rename eligible1overall elect1                                                  //renaming vars to be consistent with all other country data sets
rename eligible1women electf1
rename castedvotes1overall votes1
rename castedvotes1women votesf1
rename approvedvotes1 validv1
rename eligible2overall elect2                                                   
rename eligible2women electf2
rename castedvotes2overall votes2
rename castedvotes2women votesf2
rename approvedvotes2 validv2
rename city urban
rename valgkrets id

gen electm1 = elect1-electf1                                                      //generating male electorate and votes
gen votesm1 = votes1-votesf1
gen electm2 = elect2-electf2                                                    
gen votesm2 = votes2-votesf2

gen s_pc1 = (votes1s/validv1)*100                                                 //generating percentgage of votes for each party
gen a_pc1 = (votes1a/validv1)*100
gen va_pc1 = (votes1v_a/validv1)*100
gen v_pc1 = (votes1v/validv1)*100
gen hfv_pc1 = (votes1hfv/validv1)*100
gen s_pc2 = (votes2s/validv2)*100                                                 
gen a_pc2 = (votes2a/validv2)*100
gen va_pc2 = (votes2v_a/validv2)*100
gen v_pc2 = (votes2v/validv2)*100
gen hfv_pc2 = (votes2hfv/validv2)*100

gen turnm_pc1 = (votesm1/electm1)*100                                              //generating turnout by sex 
gen turnf_pc1 = (votesf1/electf1)*100
gen turnm_pc2 = (votesm2/electm2)*100                                              
gen turnf_pc2 = (votesf2/electf2)*100
gen turn_pc2 = (votes2/elect2)*100
gen turn_pc1 = (votes1/elect1)*100

drop if id>3000                                                                    //dropping multi-district municipalities

drop if validv1-(votes1s +votes1a +votes1v_a +votes1v +votes1oth +votes1hfv)  >0   //dropping 1 district where sum of votes exceeds valid votes, also true for round 2                                          

egen max1 = rowmax(s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1)                               //generating margin between top two candidates
rowsort s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin1 = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2 

egen max1 = rowmax(s_pc2 a_pc2 va_pc2 v_pc2 hfv_pc2)                                 
rowsort s_pc2 a_pc2 va_pc2 v_pc2 hfv_pc2, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin2 = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2 

gen turnf_pc = turnf_pc1                                                           //generating relevant variables in decisive round                                                    
replace turnf_pc = turnf_pc2 if turnf_pc2!=.
gen turnm_pc = turnm_pc1                                                         
replace turnm_pc = turnm_pc2 if turnm_pc2!=.
gen turn_pc = turn_pc1                                                         
replace turn_pc = turn_pc2 if turn_pc2!=.
gen turng = turnf_pc-turnm_pc
gen margin = margin1                                                         
replace margin = margin2 if margin2!=.
gen elect = elect1                                                         
replace elect = elect2 if elect2!=0
gen electm = electm1                                                         
replace electm = electm2 if electm2!=0
gen electf = electf1                                                         
replace electf = electf2 if electf2!=0
gen votes = votes1                                                         
replace votes = votes2 if votes2!=0
gen votesm = votesm1                                                         
replace votesm = votesm2 if votesm2!=0
gen votesf = votesf1                                                         
replace votesf = votesf2 if votesf2!=0

gen el_rat = electm/electf                                                      //generating sex ratio of electorate
gen wsh = (votesf/(votesm+ votesf))*100                                          //generating women's share of voters                                                   

gen s_pc = s_pc1                                                         
replace s_pc = s_pc2 if s_pc2!=0
gen a_pc = a_pc1                                                         
replace a_pc = a_pc2 if a_pc2!=0
gen va_pc = va_pc1                                                         
replace va_pc = va_pc2 if va_pc2!=0
gen v_pc = v_pc1                                                         
replace v_pc = v_pc2 if v_pc2!=0
gen hfv_pc = hfv_pc1                                                         
replace hfv_pc = hfv_pc2 if hfv_pc2!=0

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09.dta", replace

*adding 1910 census
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("norcens10") firstrow case(lower) clear
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norcens10.dta", replace

import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor09_distr") firstrow case(lower) clear
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norcens10.dta"
 Result                           # of obs.
    -----------------------------------------
    not matched                            33
        from master                        19  (_merge==1) //multi-district municipalities  
        from using                         14  (_merge==2) //14 municipalities in census 1910 that do not appear in 1909 election - need to be manually reassigned, see below
    matched                               643  (_merge==3)
    -----------------------------------------

drop if knr>3000                                          //following Cox et al 2016: dropping 19 multi-district municipalities

replace valgkrets=104 if knr==112                         //manually assigning municipalities that were split in 1910 into 1909 districts
replace valgkrets=503 if knr==526
replace valgkrets=1205 if knr==1252
replace valgkrets=1204 if knr==1259
replace valgkrets=1204 if knr==1264
replace valgkrets=1404 if knr==1439
replace valgkrets=1404 if knr==1440
replace valgkrets=1701 if knr==1701

drop if knr==301                                          //dropping split municipalities that belong to (dropped) multi-district municipalities         
drop if knr==602	
drop if knr==1001
drop if knr==1103
drop if knr==1301
drop if knr==1601
drop if knr==1701	

drop _merge
collapse (sum) minmanuf intel marwom15 adultwom15 adultmen15, by(valgkrets)	
gen intel10 = (intel/(adultwom15+adultmen15))*100
gen minmanuf10 = (minmanuf/(adultwom15+adultmen15))*100
gen marw10 = (marwom15/adultwom15)*100
rename valgkrets id
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_censdistr.dta", replace

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09.dta"
merge 1:1 id using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_censdistr.dta"
    Result                           # of obs.
    -----------------------------------------
    not matched                             1
        from master                         0  (_merge==1)
        from using                          1  (_merge==2)   //district dropped above for data inconsistencies

    matched                               103  (_merge==3)
    -----------------------------------------

keep if _merge==3
drop _merge
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09.dta", replace

*generating contest indicators
egen max1 = rowmax(s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1)                               //generating indicators for a frontrunner and runner up candidate for figure A6
rowsort s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)

gen ConLib1 = .                                                                    
replace ConLib1 = 1 if max1==hfv_pc1 & max2==v_pc1
replace ConLib1 = 1 if max2==hfv_pc1 & max1==v_pc1

gen ConSoc1 = .
replace ConSoc1 = 1 if max1==hfv_pc1 & max2==s_pc1
replace ConSoc1 = 1 if max2==hfv_pc1 & max1==s_pc1

gen LibSoc1 = .
replace LibSoc1 = 1 if max1==v_pc1 & max2==s_pc1
replace LibSoc1 = 1 if max2==v_pc1 & max1==s_pc1

drop  max1 p1 p2 p3 p4 p5 max2 

gen soccont = 0                                                                      //generting indicator if districts where socialist candidate is a contender (first or second) in the first round
replace soccont = 1 if ConSoc1==1
replace soccont = 1 if LibSoc1==1

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09.dta", replace


*****************************************************
*****************************************************
**              CREATING NOR12.DTA                 **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor12") firstrow case(lower) clear

replace votes1oth = votes1oth+ votes1kir + votes1t + votes1spr + votes1l        //adding  small parties (less than 1.5%) to `other' category"
drop  votes1spr votes1kir votes1t votes1l
generate votes1hfv = votes1h +votes1h_fv + votes1fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1918
drop votes1h votes1h_fv  votes1fv 
replace votes2oth = votes2oth+ votes2kir + votes2t + votes2spr + votes2l        //adding  small parties (less than 1.5%) to `other' category"
drop  votes2spr votes2kir votes2t votes2l
generate votes2hfv = votes2h +votes2h_fv + votes2fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1918
drop votes2h votes2h_fv  votes2fv 

rename castedvotes1overall votes1
rename castedvotes1women votesf1
rename approvedvotes1 validv1
rename castedvotes2overall votes2
rename castedvotes2women votesf2
rename approvedvotes2 validv2
rename eligible1overall  elect1
rename eligible1women  electf1
rename eligible2overall  elect2
rename eligible2women  electf2
rename city urban
gen districtSMD = valgkrets                                                
gen id = districtSMD

gen electm1 = elect1-electf1
gen votesm1 = votes1-votesf1
gen electm2 = elect2-electf2
gen votesm2 = votes2-votesf2

gen s_pc1 = (votes1s/validv1)*100                                               //generating percentgage of votes for each party
gen a_pc1 = (votes1a/validv1)*100
gen va_pc1 = (votes1v_a/validv1)*100
gen v_pc1 = (votes1v/validv1)*100
gen hfv_pc1 = (votes1hfv/validv1)*100
gen s_pc2 = (votes2s/validv2)*100                                               //generating percentgage of votes for each party
gen a_pc2 = (votes2a/validv2)*100
gen va_pc2 = (votes2v_a/validv2)*100
gen v_pc2 = (votes2v/validv2)*100
gen hfv_pc2 = (votes2hfv/validv2)*100                              

drop if districtSMD>3000                                                        //dropping 20 multi-district municipalities

egen max1 = rowmax(s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1)                            //generating margin between top two candidates
rowsort s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin121 = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2 

egen max1 = rowmax(s_pc2 a_pc2 va_pc2 v_pc2 hfv_pc2)                            //generating margin between top two candidates
rowsort s_pc2 a_pc2 va_pc2 v_pc2 hfv_pc2, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin122 = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2 

gen margin1 = margin121  
gen margin2 = margin122                                                       
gen margin = margin121                                                         
replace margin = margin122 if margin122!=.

gen elect = elect1                                                              //generating final round electorate and votes for replication of Figure 2                                
replace elect = elect2 if elect2!=0
gen electm = electm1                                                         
replace electm = electm2 if electm2!=0
gen electf = electf1                                                         
replace electf = electf2 if electf2!=0
gen votes = votes1                                                         
replace votes = votes2 if votes2!=0
gen votesm = votesm1                                                         
replace votesm = votesm2 if votesm2!=0
gen votesf = votesf1                                                         
replace votesf = votesf2 if votesf2!=0

gen turnm_pc = (votesm/electm)*100                                           //generating turnout by sex 
gen turnf_pc = (votesf/electf)*100
gen turng = turnf_pc-turnm_pc                                               //generating gender turnout gap
gen turn_pc = (votes/elect)*100
gen wsh = (votesf/(votesm+ votesf))*100                                          //generating women's share of voters                                                   

keep year id districtSMD wsh urban margin  votesf votesm votes electf electm elect turnm_pc turnf_pc turng 
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor12.dta"	, replace	
	

*****************************************************
*****************************************************
**              CREATING NOR15.DTA                 **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor15") firstrow case(lower) clear

replace votes1oth = votes1oth+ votes1kir + votes1t + votes1spr + votes1l        //adding  small parties (less than 1.5%) to `other' category"
drop  votes1spr votes1kir votes1t votes1l
generate votes1hfv = votes1h +votes1h_fv + votes1fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1915
drop votes1h votes1h_fv  votes1fv 
replace votes2oth = votes2oth+ votes2kir + votes2t + votes2spr + votes2l        
drop  votes2spr votes2kir votes2t votes2l
generate votes2hfv = votes2h +votes2h_fv + votes2fv                           
drop votes2h votes2h_fv  votes2fv 

rename eligible1overall elect1                                                  //renaming vars to be consistent with all other country data sets
rename eligible1women electf1
rename castedvotes1overall votes1
rename castedvotes1women votesf1
rename approvedvotes1 validv1
rename eligible2overall elect2                                                   
rename eligible2women electf2
rename castedvotes2overall votes2
rename castedvotes2women votesf2
rename approvedvotes2 validv2
rename city urban
rename valgkrets id

gen electm1 = elect1-electf1                                                      //generating male electorate and votes
gen votesm1 = votes1-votesf1
gen electm2 = elect2-electf2                                                    
gen votesm2 = votes2-votesf2

gen s_pc1 = (votes1s/validv1)*100                                                 //generating percentgage of votes for each party
gen a_pc1 = (votes1a/validv1)*100
gen va_pc1 = (votes1v_a/validv1)*100
gen v_pc1 = (votes1v/validv1)*100
gen hfv_pc1 = (votes1hfv/validv1)*100
gen s_pc2 = (votes2s/validv2)*100                                                 //generating percentgage of votes for each party
gen a_pc2 = (votes2a/validv2)*100
gen va_pc2 = (votes2v_a/validv2)*100
gen v_pc2 = (votes2v/validv2)*100
gen hfv_pc2 = (votes2hfv/validv2)*100

gen turnm_pc1 = (votesm1/electm1)*100                                              //generating turnout by sex 
gen turnf_pc1 = (votesf1/electf1)*100
gen turnm_pc2 = (votesm2/electm2)*100                                              
gen turnf_pc2 = (votesf2/electf2)*100
gen turn_pc2 = (votes2/elect2)*100
gen turn_pc1 = (votes1/elect1)*100

drop if id>3000                                                                    //dropping 20 multi-district municipalities
drop if turnm_pc2>100 & turnm_pc2<.                                                //dropping one district where men's turnout exceeds 100 

egen max1 = rowmax(s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1)                               //generating margin between top two candidates
rowsort s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin1 = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2 

egen max1 = rowmax(s_pc2 a_pc2 va_pc2 v_pc2 hfv_pc2)                                 
rowsort s_pc2 a_pc2 va_pc2 v_pc2 hfv_pc2, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin2 = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2

gen turnf_pc = turnf_pc1                                                         //generating relevant vars in final round if that exists                                                     
replace turnf_pc = turnf_pc2 if turnf_pc2!=.
gen turnm_pc = turnm_pc1                                                         
replace turnm_pc = turnm_pc2 if turnm_pc2!=.
gen margin = margin1                                                         
replace margin = margin2 if margin2!=.
gen elect = elect1                                                         
replace elect = elect2 if elect2!=0
gen electm = electm1                                                         
replace electm = electm2 if electm2!=0
gen electf = electf1                                                         
replace electf = electf2 if electf2!=0
gen votes = votes1                                                         
replace votes = votes2 if votes2!=0
gen votesm = votesm1                                                         
replace votesm = votesm2 if votesm2!=0
gen votesf = votesf1                                                         
replace votesf = votesf2 if votesf2!=0
gen turng = turnf_pc-turnm_pc
gen turn_pc = turn_pc1                                                         
replace turn_pc = turn_pc2 if turn_pc2!=.

gen wsh = (votesf/(votesm+ votesf))*100                                          //generating women's share of voters                                                   
gen el_rat = electm/electf                                                      //generating sex ratio of electorate

gen hfv_pc = hfv_pc1                                                            //generating percentgage of votes for each party                                                
replace hfv_pc = hfv_pc2 if hfv_pc2!=0
gen v_pc = v_pc1                                                         
replace v_pc = v_pc2 if v_pc2!=0
gen va_pc = va_pc1                                                         
replace va_pc = va_pc2 if va_pc2!=0
gen s_pc = s_pc1                                                         
replace s_pc = s_pc2 if s_pc2!=0
gen a_pc = a_pc1                                                         
replace a_pc = a_pc2 if a_pc2!=0

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15.dta", replace

*adding 1910 census
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor15_distr") firstrow case(lower) clear
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norcens10.dta"

    Result                           # of obs.
    -----------------------------------------
    not matched                            52
        from master                        44  (_merge==1) //multi-district municipalities and split municipalities between 1910 and 1915 - to be dropped, see below 
        from using                          8  (_merge==2) //split munic that belonged to dropped multi-district municipalities
		matched                           649  (_merge==3)
    -----------------------------------------
drop if knr>3000                                           //dropping multi-district municipalities

drop if knr==301                                           //dropping split municipalities that belong to (dropped) multi-district municipalities         
drop if knr==602	
drop if knr==1001
drop if knr==1103
drop if knr==1601
drop if knr==1701	
drop if knr==1280	
drop if knr==1301	

drop if _merge==1                                          //dropping split municipalities between 1910 (census) and 1915 (election)
drop _merge
collapse (sum) minmanuf intel marwom15 adultwom15  adultmen15 , by(valgkrets)	
gen intel10 = (intel/(adultwom15+adultmen15))*100
gen minmanuf10 = (minmanuf/(adultwom15+adultmen15))*100
gen marw10 = (marwom15/adultwom15)*100
rename valgkrets id
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_censdistr.dta", replace

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15.dta"
merge 1:1 id using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_censdistr.dta"
    Result                           # of obs.
    -----------------------------------------
    not matched                             1
        from master                         0  (_merge==1)
        from using                          1  (_merge==2)   //district dropped above for data inconsistencies

    matched                               103  (_merge==3)
    -----------------------------------------

keep if _merge==3
drop _merge
                                               
gen reshuf1015 = 0 //marking districts with reshuffled municipalities anytime between 1910 (census data) and 1915 (election data) 
replace reshuf1015=1 if id==1001
replace reshuf1015=1  if id==690
replace reshuf1015=1  if id==1901
replace reshuf1015=1  if id==1505
replace reshuf1015=1  if id==1504
replace reshuf1015=1  if id==1502

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15.dta", replace

*generating contest indicators
egen max1 = rowmax(s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1)                               //generating indicators for a frontrunner and runner up candidate for figure A6
rowsort s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)

gen ConLib1 = .                                                                    
replace ConLib1 = 1 if max1==hfv_pc1 & max2==v_pc1
replace ConLib1 = 1 if max2==hfv_pc1 & max1==v_pc1

gen ConSoc1 = .
replace ConSoc1 = 1 if max1==hfv_pc1 & max2==s_pc1
replace ConSoc1 = 1 if max2==hfv_pc1 & max1==s_pc1

gen LibSoc1 = .
replace LibSoc1 = 1 if max1==v_pc1 & max2==s_pc1
replace LibSoc1 = 1 if max2==v_pc1 & max1==s_pc1

drop  max1 p1 p2 p3 p4 p5 max2 

replace ConLib1=. if id==1405                                                       //manually correcting  two districts which had a single frontrunner among the five largest parties 
replace ConLib1=. if id==1504
replace LibSoc1=. if id==1405
replace LibSoc1=. if id==1504

gen soccont = 0                                                                      //generting indicator if districts where socialist candidate is a contender (first or second) in the first round
replace soccont = 1 if ConSoc1==1
replace soccont = 1 if LibSoc1==1

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15.dta", replace


*****************************************************
*****************************************************
**              CREATING NOR18.DTA                 **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor18") firstrow case(lower) clear

replace votes1oth = votes1oth+ votes1kir + votes1t + votes1spr + votes1l        //adding  small parties (less than 1.5%) to `other' category"
drop  votes1spr votes1kir votes1t votes1l
generate votes1hfv = votes1h +votes1h_fv + votes1fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1918
drop votes1h votes1h_fv  votes1fv 
replace votes2oth = votes2oth+ votes2kir + votes2t + votes2spr + votes2l        //adding  small parties (less than 1.5%) to `other' category"
drop  votes2spr votes2kir votes2t votes2l
generate votes2hfv = votes2h +votes2h_fv + votes2fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1918
drop votes2h votes2h_fv  votes2fv 

rename eligible1overall elect1                                                  //renaming vars to be consistent with all other  data sets
rename eligible1women electf1
rename castedvotes1overall votes1
rename castedvotes1women votesf1
rename approvedvotes1 validv1
rename eligible2overall elect2                                                  //renaming vars to be consistent with all other  data sets
rename eligible2women electf2
rename castedvotes2overall votes2
rename castedvotes2women votesf2
rename approvedvotes2 validv2
rename city urban
gen districtSMD = valgkrets                                                

gen electm1 = elect1-electf1                                                    //generating male electorate and votes
gen votesm1 = votes1-votesf1
gen electm2 = elect2-electf2                                                    //generating male electorate and votes
gen votesm2 = votes2-votesf2

gen s_pc1 = (votes1s/validv1)*100                                               //generating percentgage of votes for each party
gen a_pc1 = (votes1a/validv1)*100
gen va_pc1 = (votes1v_a/validv1)*100
gen v_pc1 = (votes1v/validv1)*100
gen hfv_pc1 = (votes1hfv/validv1)*100
gen s_pc2 = (votes2s/validv2)*100                                               //generating percentgage of votes for each party
gen a_pc2 = (votes2a/validv2)*100
gen va_pc2 = (votes2v_a/validv2)*100
gen v_pc2 = (votes2v/validv2)*100
gen hfv_pc2 = (votes2hfv/validv2)*100

gen turnm_pc1 = (votesm1/electm1)*100                                           //generating turnout by sex 
gen turnf_pc1 = (votesf1/electf1)*100
gen turn_pc1 = (votes1/elect1)*100                                           
gen turnm_pc2 = (votesm2/electm2)*100                                             
gen turnf_pc2 = (votesf2/electf2)*100
gen turn_pc2 = (votes2/elect2)*100                                           

drop if districtSMD>3000                                                        //following Cox et al 2016: dropping 20 multi-district municipalities

egen max1 = rowmax(s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1)                            //generating margin between top two candidates
rowsort s_pc1 a_pc1 va_pc1 v_pc1 hfv_pc1, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin181 = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2 

egen max1 = rowmax(s_pc2 a_pc2 va_pc2 v_pc2 hfv_pc2)                            //generating margin between top two candidates
rowsort s_pc2 a_pc2 va_pc2 v_pc2 hfv_pc2, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin182 = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2 

gen turn_pc = turn_pc1                                                          //generating turnout and margin in decisive round
replace turn_pc = turn_pc2 if turn_pc2!=.
gen turnf_pc = turnf_pc1                                                         
replace turnf_pc = turnf_pc2 if turnf_pc2!=.
gen turnm_pc = turnm_pc1                                                         
replace turnm_pc = turnm_pc2 if turnm_pc2!=.
gen turng = turnf_pc - turnm_pc                                                         

gen margin1 = margin181    
gen margin2 = margin182                                                     
gen margin18 = margin181                                                         
gen margin18f = margin181                                                         
replace margin18f = margin182 if margin182!=.
gen margin= margin18f

gen elect = elect1                                                              //generating final round electorate and votes for replication of Figure 2                                
replace elect = elect2 if elect2!=0
gen electm = electm1                                                         
replace electm = electm2 if electm2!=0
gen electf = electf1                                                         
replace electf = electf2 if electf2!=0
gen votes = votes1                                                         
replace votes = votes2 if votes2!=0
gen votesm = votesm1                                                         
replace votesm = votesm2 if votesm2!=0
gen votesf = votesf1                                                         
replace votesf = votesf2 if votesf2!=0

gen wsh = (votesf/(votesm+ votesf))*100                                         //generating women's share of voters                                                      
gen id = districtSMD

keep year districtSMD id wsh urban turn_pc turnm_pc turnf_pc turng margin  elect electm electf votes votesm votesf  
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18.dta"	, replace


*****************************************************
*****************************************************
**          CREATING NOR0918.DTA                   **
*****************************************************
*****************************************************	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09.dta"
preserve
append  using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor12.dta"	
append  using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15.dta"	
append  using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18.dta"	
centile margin , c( 0 10 20 30 40 50 60 70 80 90 100)    //generating centiles of Margin                
ret li
gen margin_c="10" if  margin>=`r(c_1)' & margin<=`r(c_2)'
replace margin_c="20" if margin>`r(c_2)' & margin<=`r(c_3)'
replace margin_c="30" if margin>`r(c_3)' & margin<=`r(c_4)'
replace margin_c="40" if margin>`r(c_4)' & margin<=`r(c_5)'
replace margin_c="50" if margin>`r(c_5)' & margin<=`r(c_6)'
replace margin_c="60" if margin>`r(c_6)' & margin<=`r(c_7)'
replace margin_c="70" if margin>`r(c_7)' & margin<=`r(c_8)'
replace margin_c="80" if margin>`r(c_8)' & margin<=`r(c_9)'
replace margin_c="90" if margin>`r(c_9)' & margin<=`r(c_10)'
replace margin_c="100" if margin>`r(c_10)' & margin<=`r(c_11)'
gen margin_10 = .
replace margin_10 = 1 if margin_c=="10"
replace margin_10 = 2 if margin_c=="20"
replace margin_10 = 3 if margin_c=="30"
replace margin_10 = 4 if margin_c=="40"
replace margin_10 = 5 if margin_c=="50"
replace margin_10 = 6 if margin_c=="60"
replace margin_10 = 7 if margin_c=="70"
replace margin_10 = 8 if margin_c=="80"
replace margin_10 = 9 if margin_c=="90"
replace margin_10 = 10 if margin_c=="100"
drop margin_c
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor0918.dta", replace
restore	
	
	
*****************************************************
*****************************************************
**          CREATING NOR0918_red.DTA               **
*****************************************************
*****************************************************	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09.dta"
preserve
append  using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor12.dta"	
append  using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15.dta"	
append  using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18.dta"	
drop if id==892                                                                 //dropping districts with reshuffled municipalities anytime between 1909 and 1918 
drop if id==1001                                                         
drop if id==690
drop if id==1901
drop if id==1505
drop if id==1504
drop if id==1502
drop if id==490
drop if id==1501                                                   
drop if id==1503
drop if id==1506
drop if id==1902
drop if id==1903
drop if id==1904
drop if id==400                                                           
drop if id==500
xtset id year
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor0918_red.dta", replace
restore


*****************************************************
*****************************************************
**            CREATING NOR0906.DTA                 **
*****************************************************
*****************************************************	
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor06_munic") firstrow case(lower) clear

gen votess = p + o + n + finalelectionvalidvotessam
drop p  o  n  finalelectionvalidvotessam
rename finalelectionvalidvotesoth votesoth
rename finalelectionvalidvotessos votessos
rename finalelectionvalidvotesarb votesa
rename finalelectionvalidvotesven votesv
rename l votesv_s  

rename stelectiontotalnumberofp elect1
rename ndelectionreelectiontot elect2
rename stelectiontotalvotescast votes1
rename ndelectiontotalvotescast votes2
rename stelectionvalidvotestotal validv1
rename ndelectionvalidvotestotal validv2

gen validv  = validv1                                                           //generating decisive round totals
replace validv = validv2 if validv2!=0
gen elect  = elect1
replace elect = elect2 if elect2!=0	
gen votes  = votes1
replace votes = votes2 if votes2!=0		

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor06.dta", replace

*merging with 1906 district codes
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor06_distr") firstrow case(lower) clear
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor06_distr.dta", replace

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor06.dta"
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor06_distr.dta"	
   Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                               629  (_merge==3)
    -----------------------------------------
drop _merge
drop if valgkrets ==.                                                           //dropping 8 multi-district municipalities

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor06.dta", replace

*collapsing on 1906 electoral districts
collapse (sum) elect  votes validv votess votesoth votesv_s votesv votesa votessos   , by(valgkrets)	

gen vs_pc = (votesv_s/validv)*100                                               //generating percentage of votes for each party in the final round
gen v_pc = (votesv/validv)*100
gen a_pc = (votesa/validv)*100
gen sos_pc = (votessos/validv)*100
gen s_pc = (votess/validv)*100
	                                   
gen turn_pc = (votes/elect)*100    
                                                                           
egen max1 = rowmax(vs_pc v_pc a_pc sos_pc s_pc )                                //generating margin between top two candidates in final round
rowsort vs_pc v_pc a_pc sos_pc s_pc , gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin06 = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2 vs_pc v_pc a_pc sos_pc s_pc 

rename valgkrets id                                                             //to enable merger with 1909
keep  id margin06  

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor06.dta", replace

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09.dta"
preserve
merge 1:1 id using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor06.dta"
    Result                           # of obs.
    -----------------------------------------
    not matched                             6
        from master                         1  (_merge==1)   
        from using                          5  (_merge==2)  

    matched                               102  (_merge==3)
    -----------------------------------------

drop if _merge==1 //these are the districts that changed between 1906 and 1909
drop if _merge==2 
drop _merge

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor0906.dta", replace
restore
	

*****************************************************
*****************************************************
**         CREATING NOR09_munic.DTA                **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor09_munic") firstrow case(lower) clear

replace votes1oth = votes1oth+ votes1kir + votes1t                              //adding  small parties (less than 1.5%) to `other' category"
drop   votes1kir votes1t
generate votes1hfv = votes1h +votes1h_fv + votes1fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1909
drop votes1h votes1h_fv  votes1fv 
replace votes2oth = votes2oth+ votes2kir + votes2t                  
drop   votes2kir votes2t
generate votes2hfv = votes2h +votes2h_fv + votes2fv                            
drop votes2h votes2h_fv  votes2fv 

rename eligible1overall elect1                                                  //renaming vars to be consistent with all other country data sets
rename eligible1women electf1
rename castedvotes1overall votes1
rename castedvotes1women votesf1
rename approvedvotes1 validv1
rename eligible2overall elect2                                                   
rename eligible2women electf2
rename castedvotes2overall votes2
rename castedvotes2women votesf2
rename approvedvotes2 validv2

foreach v of varlist elect1 electf1 elect2 electf2 votes1 votesf1 votes2 votesf2 validv1 validv2 votes1s votes1a votes1v_a votes1v votes1spr votes1oth votes2s votes2a votes2v_a votes2v votes2spr votes2oth votes1hfv votes2hfv {
replace `v' = . if `v'==9999999
}

gen electm1 = elect1-electf1                                                      //generating male electorate and votes
gen votesm1 = votes1-votesf1
gen electm2 = elect2-electf2                                                    
gen votesm2 = votes2-votesf2

gen s_pc1 = (votes1s/validv1)*100                                                 //generating percentgage of votes for each party
gen a_pc1 = (votes1a/validv1)*100
gen va_pc1 = (votes1v_a/validv1)*100
gen v_pc1 = (votes1v/validv1)*100
gen hfv_pc1 = (votes1hfv/validv1)*100
gen oth_pc1 = (votes1oth/validv1)*100
gen s_pc2 = (votes2s/validv2)*100                                                 //generating percentgage of votes for each party
gen a_pc2 = (votes2a/validv2)*100
gen va_pc2 = (votes2v_a/validv2)*100
gen v_pc2 = (votes2v/validv2)*100
gen hfv_pc2 = (votes2hfv/validv2)*100
gen oth_pc2 = (votes2oth/validv2)*100

gen turnm_pc1 = (votesm1/electm1)*100                                              //generating turnout by sex 
gen turnf_pc1 = (votesf1/electf1)*100
gen turnm_pc2 = (votesm2/electm2)*100                                              
gen turnf_pc2 = (votesf2/electf2)*100
gen turn_pc2 = (votes2/elect2)*100
gen turn_pc1 = (votes1/elect1)*100

gen turnf_pc = turnf_pc1                                                           //generating relevant variables in decisive round                                                     
replace turnf_pc = turnf_pc2 if turnf_pc2!=.
gen turnm_pc = turnm_pc1                                                         
replace turnm_pc = turnm_pc2 if turnm_pc2!=.
gen turn_pc = turn_pc1                                                         
replace turn_pc = turn_pc2 if turn_pc2!=.
gen turng = turnf_pc-turnm_pc

gen elect = elect1                                                         
replace elect = elect2 if elect2!=0
gen electm = electm1                                                         
replace electm = electm2 if electm2!=0
gen electf = electf1                                                         
replace electf = electf2 if electf2!=0
gen votes = votes1                                                         
replace votes = votes2 if votes2!=0
gen votesm = votesm1                                                         
replace votesm = votesm2 if votesm2!=0
gen votesf = votesf1                                                         
replace votesf = votesf2 if votesf2!=0
gen s_pc = s_pc1                                                         
replace s_pc = s_pc2 if s_pc2!=.
gen a_pc = a_pc1                                                         
replace a_pc = a_pc2 if a_pc2!=.
gen va_pc = va_pc1                                                         
replace va_pc = va_pc2 if va_pc2!=.
gen v_pc = v_pc1                                                         
replace v_pc = v_pc2 if v_pc2!=.
gen hfv_pc = hfv_pc1                                                         
replace hfv_pc = hfv_pc2 if hfv_pc2!=.
gen oth_pc = oth_pc1                                                         
replace oth_pc = oth_pc2 if oth_pc2!=.

replace s_pc = (s_pc/100)^2                                                 //generating HHI
replace a_pc = (a_pc/100)^2 
replace v_pc = (v_pc/100)^2 
replace va_pc = (va_pc/100)^2 
replace hfv_pc = (hfv_pc/100)^2 
replace oth_pc = (oth_pc/100)^2 
gen hhi = (s_pc + a_pc  + v_pc + hfv_pc + va_pc + oth_pc)*100
drop s_pc a_pc  v_pc hfv_pc va_pc oth_pc

drop if elect<votes        //dropping N=0 where electorate size is smaller than valid votes in a final round
drop if electm<votesm      //dropping N=3 where electorate size is smaller than valid votes in a final round
drop if electf<votesf      //dropping N=2 where electorate size is smaller than valid votes in a final round
drop if electm1<votesm1    //dropping N=1 where electorate size is smaller than valid votes in first round
  
gen sumvot1 = votes1s +votes1a +votes1v_a +votes1v +votes1oth +votes1hfv+votes1spr
gen sumvot2 = votes2s +votes2a +votes2v_a +votes2v +votes2oth +votes2hfv+votes2spr
gen sumvot = sumvot1                                                         
replace sumvot = sumvot2 if sumvot2!=0  
drop if sumvot1>votes1      //dropping N=1 municipality where sum of votes exceeds total votes in first round                                         
drop if sumvot>votes        //dropping N=1 municipality where sum of votes exceeds total votes in final round                                         
drop sumvot1 sumvot2

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_munic.dta", replace

*adding districts 1909
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor09_distr") firstrow case(lower) clear
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_munic.dta"

 Result                           # of obs.
    -----------------------------------------
    not matched                            34
        from master                        27  (_merge==1) //19 multi-district municipalities & 8 municipalities excluded because of innacurancies above
        from using                          7  (_merge==2) //split municipalities that belonged to dropped multidistrict municipalities, dropped below
    matched                               635  (_merge==3)
    -----------------------------------------
	
drop if knr>3000                                          //dropping 19 multi-district municipalities

drop if knr==301                                          //dropping split municipalities that belong to (dropped) multi-district municipalities         
drop if knr==602	
drop if knr==1001
drop if knr==1103
drop if knr==1301
drop if knr==1601
drop if knr==1701	

drop if _merge==1                                          //dropping 8 municipalities with innacurate data, as above 
drop _merge 
 
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_munic.dta", replace
 
*adding competition at district level
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09.dta", replace
preserve
keep id margin1 margin
rename id valgkrets
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_dcomp.dta", replace
restore

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_munic.dta", replace
merge m:1 valgkrets using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_dcomp.dta"

 Result                           # of obs.
    -----------------------------------------
    not matched                             8
        from master                         8  (_merge==1)
        from using                          0  (_merge==2)

    matched                               627  (_merge==3)
    -----------------------------------------

	
drop if _merge==1                                         
drop _merge 
 
bys valgkrets: gen tmunic_d = _N                //generating number of municipalitie in a district
 
centile margin , c( 0 50 100)  
ret li
gen     margin_2=0 if margin>=`r(c_1)' & margin<=`r(c_2)'
replace margin_2=1 if margin>`r(c_2)' & margin<=`r(c_3)'
label define lohi 0 "Low" 1 "High"
label val  margin_2 lohi

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_munic.dta", replace
 
 
*****************************************************
*****************************************************
**         CREATING NOR15_munic.DTA                **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor15_munic") firstrow case(lower) clear

replace votes1oth = votes1oth+ votes1kir + votes1t + votes1l                    //adding  small parties (less than 1.5%) to `other' category"
drop   votes1kir votes1t votes1l
generate votes1hfv = votes1h +votes1h_fv + votes1fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1909
drop votes1h votes1h_fv  votes1fv 
replace votes2oth = votes2oth+ votes2kir + votes2t + votes2l                
drop   votes2kir votes2t votes2l
generate votes2hfv = votes2h +votes2h_fv + votes2fv                            
drop votes2h votes2h_fv  votes2fv 

rename eligible1overall elect1                                                  //renaming vars to be consistent with all other country data sets, also allows running the same commands
rename eligible1women electf1
rename castedvotes1overall votes1
rename castedvotes1women votesf1
rename approvedvotes1 validv1
rename eligible2overall elect2                                                   
rename eligible2women electf2
rename castedvotes2overall votes2
rename castedvotes2women votesf2
rename approvedvotes2 validv2

foreach v of varlist elect1 electf1 elect2 electf2 votes1 votesf1 votes2 votesf2 validv1 validv2 votes1s votes1a votes1v_a votes1v votes1spr votes1oth votes2s votes2a votes2v_a votes2v votes2spr votes2oth votes1hfv votes2hfv {
replace `v' = . if `v'==9999999
}

gen electm1 = elect1-electf1                                                      //generating male electorate and votes
gen votesm1 = votes1-votesf1
gen electm2 = elect2-electf2                                                    
gen votesm2 = votes2-votesf2

gen s_pc1 = (votes1s/validv1)*100                                                 //generating percentgage of votes for each party
gen a_pc1 = (votes1a/validv1)*100
gen va_pc1 = (votes1v_a/validv1)*100
gen v_pc1 = (votes1v/validv1)*100
gen hfv_pc1 = (votes1hfv/validv1)*100
gen s_pc2 = (votes2s/validv2)*100                                                 //generating percentgage of votes for each party
gen a_pc2 = (votes2a/validv2)*100
gen va_pc2 = (votes2v_a/validv2)*100
gen v_pc2 = (votes2v/validv2)*100
gen hfv_pc2 = (votes2hfv/validv2)*100
gen oth_pc2 = (votes2oth/validv2)*100
gen oth_pc1 = (votes1oth/validv1)*100


gen turnm_pc1 = (votesm1/electm1)*100                                              //generating turnout by sex 
gen turnf_pc1 = (votesf1/electf1)*100
gen turnm_pc2 = (votesm2/electm2)*100                                              
gen turnf_pc2 = (votesf2/electf2)*100
gen turn_pc2 = (votes2/elect2)*100
gen turn_pc1 = (votes1/elect1)*100

gen turnf_pc = turnf_pc1                                                           //generating relevant variables in final round if that exists                                                     
replace turnf_pc = turnf_pc2 if turnf_pc2!=.
gen turnm_pc = turnm_pc1                                                         
replace turnm_pc = turnm_pc2 if turnm_pc2!=.
gen turn_pc = turn_pc1                                                         
replace turn_pc = turn_pc2 if turn_pc2!=.
gen turng = turnf_pc-turnm_pc

gen elect = elect1                                                         
replace elect = elect2 if elect2!=0
gen electm = electm1                                                         
replace electm = electm2 if electm2!=0
gen electf = electf1                                                         
replace electf = electf2 if electf2!=0
gen votes = votes1                                                         
replace votes = votes2 if votes2!=0
gen votesm = votesm1                                                         
replace votesm = votesm2 if votesm2!=0
gen votesf = votesf1                                                         
replace votesf = votesf2 if votesf2!=0
gen s_pc = s_pc1                                                         
replace s_pc = s_pc2 if s_pc2!=.
gen a_pc = a_pc1                                                         
replace a_pc = a_pc2 if a_pc2!=.
gen va_pc = va_pc1                                                         
replace va_pc = va_pc2 if va_pc2!=.
gen v_pc = v_pc1                                                         
replace v_pc = v_pc2 if v_pc2!=.
gen hfv_pc = hfv_pc1                                                         
replace hfv_pc = hfv_pc2 if hfv_pc2!=.
gen oth_pc = oth_pc1                                                         
replace oth_pc = oth_pc2 if oth_pc2!=. 
  
drop if electm<votesm      //dropping N=8 where electorate size is smaller than valid votes in a final round
drop if electf<votesf      //dropping N=1 where electorate size is smaller than valid votes in a final round

gen sumvot1 = votes1s +votes1a +votes1v_a +votes1v +votes1oth +votes1hfv +votes1spr
gen sumvot2 = votes2s +votes2a +votes2v_a +votes2v +votes2oth +votes2hfv +votes2spr
gen sumvot = sumvot1                                                         
replace sumvot = sumvot2 if sumvot2!=0  
drop if sumvot>votes        //dropping N=2 municipality where sum of votes exceeds total votes in final round                                         
drop if sumvot1>votes1      //dropping N=1 municipality where sum of votes exceeds total votes in first round                                         
drop sumvot1 sumvot2
 
gen year=1915 
 
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_munic.dta", replace

*ading nor_local15 
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor_local15") firstrow case(lower) clear
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norlocal15.dta", replace

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_munic.dta", replace
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norlocal15.dta"

    Result                           # of obs.
    -----------------------------------------
    not matched                            12
        from master                         0  (_merge==1)
        from using                         12  (_merge==2)      //12 dropped above as inconsistent

    matched                               669  (_merge==3)
    -----------------------------------------
drop if _merge==2
drop _merge
replace wsoc=0 if wsoc==.
replace con_local=0 if con_local==.
label define yesno 0 "None" 1 "At Least One"
label values wsoc yesno
label values con_local yesno

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_munic.dta", replace
 
*adding district 1915
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor15_distr") firstrow case(lower) clear
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_munic.dta"

 Result                           # of obs.
    -----------------------------------------
    not matched                            38
        from master                        31  (_merge==1) //19 multi-district municipalities & rest municipalities excluded because of innacurancies above
        from using                          7  (_merge==2) //split municipalities that belonged to dropped multidistrict municipalities, dropped below
    matched                               662  (_merge==3)
    -----------------------------------------
	
drop if knr>3000                                          //dropping 19 multi-district municipalities
drop if knr==301                                          //dropping split municipalities that belong to (dropped) multi-district municipalities         
drop if knr==602	
drop if knr==1001
drop if knr==1103
drop if knr==1301
drop if knr==1601
drop if knr==1701	

drop if _merge==1                                          //dropping  municipalities with innacurate data, as above 
drop _merge 
 
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_munic.dta", replace
 
*adding competition at district level
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15.dta", replace
preserve
keep id margin1 margin
rename id valgkrets
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_dcomp.dta", replace
restore

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_munic.dta", replace
merge m:1 valgkrets using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_dcomp.dta"

    Result                           # of obs.
    -----------------------------------------
    not matched                             0
    matched                               662  (_merge==3)
    -----------------------------------------
	
drop _merge 
 
bys valgkrets: gen tmunic_d = _N                //generating number of municipalitie in a district
 
replace s_pc = (s_pc/100)^2                                                 //generating HHI
replace a_pc = (a_pc/100)^2 
replace v_pc = (v_pc/100)^2 
replace va_pc = (va_pc/100)^2 
replace hfv_pc = (hfv_pc/100)^2 
replace oth_pc = (oth_pc/100)^2 
gen hhi = (s_pc + a_pc  + v_pc + hfv_pc + va_pc + oth_pc)*100
drop s_pc a_pc  v_pc hfv_pc va_pc oth_pc 
 
gen s_pc = s_pc1                                                         
replace s_pc = s_pc2 if s_pc2!=.
gen a_pc = a_pc1                                                         
replace a_pc = a_pc2 if a_pc2!=.
gen va_pc = va_pc1                                                         
replace va_pc = va_pc2 if va_pc2!=.
gen v_pc = v_pc1                                                         
replace v_pc = v_pc2 if v_pc2!=.
gen hfv_pc = hfv_pc1                                                         
replace hfv_pc = hfv_pc2 if hfv_pc2!=.
gen oth_pc = oth_pc1                                                         
replace oth_pc = oth_pc2 if oth_pc2!=. 
 
centile margin , c( 0 50 100)  
ret li
gen     margin_2=0 if margin>=`r(c_1)' & margin<=`r(c_2)'
replace margin_2=1 if margin>`r(c_2)' & margin<=`r(c_3)'
label define lohi 0 "Low" 1 "High"
label val  margin_2 lohi
 
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_munic.dta", replace
 
 
*****************************************************
*****************************************************
**         CREATING NOR18_munic.DTA                **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor18_munic") firstrow case(lower) clear

replace votes1oth = votes1oth+ votes1kir + votes1t  + votes1l        //adding  small parties (less than 1.5%) to `other' category"
drop   votes1kir votes1t votes1l
generate votes1hfv = votes1h +votes1h_fv + votes1fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1918
drop votes1h votes1h_fv  votes1fv 
replace votes2oth = votes2oth+ votes2kir + votes2t  + votes2l        //adding  small parties (less than 1.5%) to `other' category"
drop   votes2kir votes2t votes2l
generate votes2hfv = votes2h +votes2h_fv + votes2fv                             //Hoyre and Frisinnede Venstre run in a national coalition in 1918
drop votes2h votes2h_fv  votes2fv 

rename eligible1overall elect1                                                  //renaming vars to be consistent with all other country data sets
rename eligible1women electf1
rename castedvotes1overall votes1
rename castedvotes1women votesf1
rename approvedvotes1 validv1
rename eligible2overall elect2                                                   
rename eligible2women electf2
rename castedvotes2overall votes2
rename castedvotes2women votesf2
rename approvedvotes2 validv2

foreach v of varlist elect1 electf1 elect2 electf2 votes1 votesf1 votes2 votesf2 validv1 validv2 votes1s votes1a votes1v_a votes1v votes1spr votes1oth votes2s votes2a votes2v_a votes2v votes2spr votes2oth votes1hfv votes2hfv {
replace `v' = . if `v'==9999999
}

gen electm1 = elect1-electf1                                                      //generating male electorate and votes
gen votesm1 = votes1-votesf1
gen electm2 = elect2-electf2                                                    
gen votesm2 = votes2-votesf2

gen s_pc1 = (votes1s/validv1)*100                                               //generating percentgage of votes for each party
gen a_pc1 = (votes1a/validv1)*100
gen va_pc1 = (votes1v_a/validv1)*100
gen v_pc1 = (votes1v/validv1)*100
gen hfv_pc1 = (votes1hfv/validv1)*100
gen s_pc2 = (votes2s/validv2)*100                                               //generating percentgage of votes for each party
gen a_pc2 = (votes2a/validv2)*100
gen va_pc2 = (votes2v_a/validv2)*100
gen v_pc2 = (votes2v/validv2)*100
gen hfv_pc2 = (votes2hfv/validv2)*100
gen oth_pc1 = (votes1oth/validv1)*100
gen oth_pc2 = (votes2oth/validv2)*100

gen turnm_pc1 = (votesm1/electm1)*100                                              //generating turnout by sex 
gen turnf_pc1 = (votesf1/electf1)*100
gen turnm_pc2 = (votesm2/electm2)*100                                              
gen turnf_pc2 = (votesf2/electf2)*100

gen turnf_pc = turnf_pc1                                                           //generating relevant variables in decisive round                                                     
replace turnf_pc = turnf_pc2 if turnf_pc2!=.
gen turnm_pc = turnm_pc1                                                         
replace turnm_pc = turnm_pc2 if turnm_pc2!=.

gen turng = turnf_pc - turnm_pc

gen elect = elect1                                                         
replace elect = elect2 if elect2!=0
gen electm = electm1                                                         
replace electm = electm2 if electm2!=0
gen electf = electf1                                                         
replace electf = electf2 if electf2!=0
gen votes = votes1                                                         
replace votes = votes2 if votes2!=0
gen votesm = votesm1                                                         
replace votesm = votesm2 if votesm2!=0
gen votesf = votesf1                                                         
replace votesf = votesf2 if votesf2!=0

gen s_pc = s_pc1                                                         
replace s_pc = s_pc2 if s_pc2!=.
gen a_pc = a_pc1                                                         
replace a_pc = a_pc2 if a_pc2!=.
gen va_pc = va_pc1                                                         
replace va_pc = va_pc2 if va_pc2!=.
gen v_pc = v_pc1                                                         
replace v_pc = v_pc2 if v_pc2!=.
gen hfv_pc = hfv_pc1                                                         
replace hfv_pc = hfv_pc2 if hfv_pc2!=.
gen oth_pc = oth_pc1                                                         
replace oth_pc = oth_pc2 if oth_pc2!=.

replace s_pc = (s_pc/100)^2                                                 //generating HHI
replace a_pc = (a_pc/100)^2 
replace v_pc = (v_pc/100)^2 
replace va_pc = (va_pc/100)^2 
replace hfv_pc = (hfv_pc/100)^2 
replace oth_pc = (oth_pc/100)^2 
gen hhi = (s_pc + a_pc  + v_pc + hfv_pc + va_pc + oth_pc)*100
drop s_pc a_pc  v_pc hfv_pc va_pc oth_pc

drop if elect<votes        //dropping N=0 where electorate size is smaller than valid votes in a final round
drop if electm<votesm      //dropping N=7 where electorate size is smaller than valid votes in a final round
drop if electf<votesf      //dropping N=0 where electorate size is smaller than valid votes in a final round
drop if electm1<votesm1    //dropping N=1 where electorate size is smaller than valid votes in first round
  
gen sumvot1 = votes1s +votes1a +votes1v_a +votes1v +votes1oth +votes1hfv+votes1spr
gen sumvot2 = votes2s +votes2a +votes2v_a +votes2v +votes2oth +votes2hfv+votes2spr
gen sumvot = sumvot1                                                         
replace sumvot = sumvot2 if sumvot2!=0  
drop if sumvot1>votes1      //dropping N=3 municipality where sum of votes exceeds total votes in first round                                         
drop if sumvot>votes        //dropping N=1 municipality where sum of votes exceeds total votes in final round                                         
drop sumvot1 sumvot2

gen year=1918

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_munic.dta", replace

*adding census
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("norcens20") firstrow case(lower) clear
gen fact20 = ((factmen + factwom) / (adultman15+adultwom15))*100    //generating percentage industrial workers
gen intel20 = ((intelmen + intelwom) / (adultman15+adultwom15))*100 //generating percentage intellectual workers
gen marw20 = (marwom15 / adultwom15)*100                            //generating percentage married women over 15 years
gen agr20 = ((agrwom + agrmen)/(adultwom15+adultman15))*100         //generating percentage agricultural workers

gen agrfishm_pc = ((agrm+fishm)/adultman15)*100                     //generating percentage agricultural men workers
gen manualm_pc = ((manualm)/adultman15)*100
gen upmidm_pc   = ((upmidm)/adultman15)*100
gen nonmanualm_pc = ((nonmanualm)/adultman15)*100
gen othm_pc = 100-(agrfishm_pc + manualm_pc +nonmanualm_pc + upmidm_pc)

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norcens20.dta", replace

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_munic.dta", replace
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norcens20.dta"

    Result                           # of obs.
    -----------------------------------------
    not matched                            19
        from master                         0  (_merge==1)
        from using                         19  (_merge==2)      //12 dropped above as inconsistent, 7 in 1920 census, but not in 1918 election

    matched                               684  (_merge==3)
    -----------------------------------------
drop if _merge==2
drop _merge

*generating hhi demographic
replace agrfishm_pc = (agrfishm_pc/100)^2                                                 //generating HHI
replace manualm_pc = (manualm_pc/100)^2 
replace upmidm_pc = (upmidm_pc/100)^2 
replace nonmanualm_pc = (nonmanualm_pc/100)^2 
replace othm_pc = (othm_pc/100)^2 
gen hhi_occup = (agrfishm_pc + manualm_pc + upmidm_pc + nonmanualm_pc + othm_pc )*100
drop agrfishm_pc manualm_pc upmidm_pc nonmanualm_pc othm_pc

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_munic.dta", replace
 	
*adding district 1918
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor18_distr") firstrow case(lower) clear
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_munic.dta"

 Result                           # of obs.
    -----------------------------------------
    not matched                            38
        from master                        31  (_merge==1) //19 multi-district municipalities & rest municipalities excluded because of innacurancies above
        from using                          7  (_merge==2) //split municipalities that belonged to dropped multidistrict municipalities, dropped below
    matched                               677  (_merge==3)
    -----------------------------------------
	
drop if knr>3000                                          //dropping 19 multi-district municipalities
drop if knr==301                                          //dropping split municipalities that belong to (dropped) multi-district municipalities         
drop if knr==602	
drop if knr==1001
drop if knr==1103
drop if knr==1301
drop if knr==1601
drop if knr==1701	

drop if _merge==1                                          //dropping  municipalities with innacurate data, as above 
drop _merge 
 
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_munic.dta", replace
 	
	
*****************************************************
*****************************************************
**              CREATING NOR21.DTA                 **
*****************************************************
*****************************************************	
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor21") firstrow case(lower) clear

gen votesf = personalvotewomen + mailwomen
gen votesm = personalvotesmen + mailmen
drop personalvotesmen personalvotewomen personalvoteoverall mailmen mailwomen mailoverall
rename valgkrets districtPR                                                     
rename knavn munic_name
rename eligibleoverall elect                                                    //renaming vars to be consistent with all other data sets
rename eligiblewomen electf
rename eligiblemen electm
rename castedvotesoverall votes
rename approvedvotesoverall validv
rename votesh_fv voteshfv

gen dna_pc = (votesdna/validv)*100                                              //generating percentgage of votes for each party
gen nsa_pc = (votesnsa/validv)*100
gen rf_pc = (votesrf/validv)*100
gen v_pc = (votesv/validv)*100
gen hfv_pc = (voteshfv/validv)*100
gen sp_pc = (votessp/validv)*100

gen turnm_pc = (votesm/electm)*100                                              //generating turnout by sex 
gen turnf_pc = (votesf/electf)*100
gen turng = turnf_pc-turnm_pc
gen turn_pc = (votes/elect)*100
gen wsh = (votesf/(votesm+ votesf))*100                                          //generating women's share of voters                                                   
gen el_rat = electm/electf                                                      //generating sex ratio of electorate

drop if turnm_pc>100                                                                    //dropping 3 municipalities with male turnout over 100%
drop if validv-(votesdna +votesnsa +votesrf +votesv +voteshfv +votessp + votesoth)  <0 //dropping 13 municipalities where sum of votes exceeds valid votes                                          

replace dna_pc = (dna_pc/100)^2                                                 //generating HHI
replace nsa_pc = (nsa_pc/100)^2 
replace rf_pc = (rf_pc/100)^2 
replace v_pc = (v_pc/100)^2 
replace hfv_pc = (hfv_pc/100)^2 
replace sp_pc = (sp_pc/100)^2 
gen hhi = (dna_pc + nsa_pc + rf_pc + v_pc + hfv_pc + sp_pc)*100
drop dna_pc nsa_pc rf_pc v_pc hfv_pc sp_pc
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta", replace

*adding 1920 census
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norcens20.dta"
    Result                           # of obs.
    -----------------------------------------
    not matched                            16
        from master                         0  (_merge==1)
        from using                         16  (_merge==2)                      //15 municipalities that were dropped above for data inconsistencies

    matched                               688  (_merge==3)
    -----------------------------------------
*keep if _merge==3
drop _merge

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta", replace

*gen fact20 = ((factmen + factwom) / (adultman15+adultwom15))*100    //generating percentage industrial workers
*gen intel20 = ((intelmen + intelwom) / (adultman15+adultwom15))*100 //generating percentage intellectual workers
gen factw20 = (factwom / adultman15)*100    //generating percentage industrial women workers 
gen intelw20 = (intelwom / adultwom15)*100 //generating percentage intellectual women workers
*gen marw20 = (marwom15 / adultwom15)*100   

*generating hhi demographic
replace agrfishm_pc = (agrfishm_pc/100)^2                                                 //generating HHI
replace manualm_pc = (manualm_pc/100)^2 
replace upmidm_pc = (upmidm_pc/100)^2 
replace nonmanualm_pc = (nonmanualm_pc/100)^2 
replace othm_pc = (othm_pc/100)^2 
gen hhi_occup = (agrfishm_pc + manualm_pc + upmidm_pc + nonmanualm_pc + othm_pc )*100
drop agrfishm_pc manualm_pc upmidm_pc nonmanualm_pc othm_pc

*generating dominant party (for alternative mechanisms)
gen dna_pc = (votesdna/validv)*100                                              //generating percentgage of votes for each party
gen nsa_pc = (votesnsa/validv)*100
gen rf_pc = (votesrf/validv)*100
gen v_pc = (votesv/validv)*100
gen hfv_pc = (voteshfv/validv)*100
gen sp_pc = (votessp/validv)*100

egen max1_p = rowmax(dna_pc nsa_pc rf_pc v_pc hfv_pc sp_pc) 

gen soc_lead = 0                                                                 //generating socialist lead in a municipality
replace soc_lead =1 if max1==dna_pc
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta", replace

*generating alternative measure of within-district competition
egen max1 = rowmax(votesdna votesnsa votesrf votesv votessp voteshfv)                               //generating margin between top two candidates
rowsort votesdna votesnsa votesrf votesv votessp voteshfv, gen(p1-p6) descending        
foreach var of varlist p1 p2 p3 p4 p5 p6 {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5 p6)
gen margin_raw = max1-max2
drop  max1 p1 p2 p3 p4 p5 p6 max2 

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta", replace

*adding 1918 HHI
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_munic.dta", replace
preserve
gen hhi18 = hhi
keep knr hhi18
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_munic_merge.dta", replace
restore
   
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta", replace
merge 1:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_munic_merge.dta"
   
   Result                           # of obs.
    -----------------------------------------
    not matched                            26
        from master                        26  (_merge==1)                      //  munic dropped for inconsistencies in 1918
        from using                          0  (_merge==2)                      //  munic dropped for inconsistencies in 1921

    matched                               677  (_merge==3)
    -----------------------------------------
*keep if _merge==3
*drop _merge
gen dhhi = hhi-hhi18
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta", replace

   
*****************************************************
*****************************************************
**              CREATING NOR24.DTA                 **
*****************************************************
*****************************************************	
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor24") firstrow case(lower) clear

replace votesoth = votesoth + votesvil                                          //adding  small parties (less than 1.5%) to `other' category"
drop  votesvil
generate voteshfv = votesh_fv + votesfv                                         //run in a national coalition
drop votesh_fv votesfv

gen votesf = personalvotewomen + mailwomen
gen votesm = personalvotesmen + mailmen
drop personalvotesmen personalvotewomen personalvoteoverall mailmen mailwomen mailoverall
rename valgkrets districtPR                                                     
rename knr id
rename knavn munic_name
rename eligibleoverall elect                                                    //renaming vars to be consistent with all other data sets
rename eligiblewomen electf
rename eligiblemen electm
rename castedvotesoverall votes
rename approvedvotesoverall validv

gen dna_pc = (votesdna/validv)*100                                              //generating percentgage of votes for each party
gen nsa_pc = (votesnsa/validv)*100
gen rf_pc = (votesrf/validv)*100
gen v_pc = (votesv/validv)*100
gen hfv_pc = (voteshfv/validv)*100
gen sp_pc = (votessp/validv)*100
gen nkp_pc = (votesnkp/validv)*100

gen turnm_pc = (votesm/electm)*100                                              //generating turnout by sex 
gen turnf_pc = (votesf/electf)*100
gen turng = turnf_pc - turnm_pc                                                 // generating gender turnout gap
gen turn_pc = (votes/elect)*100
gen wsh = (votesf/(votesm+ votesf))*100                                          //generating women's share of voters                                                   

replace dna_pc = (dna_pc/100)^2                                                 //generating HHI
replace nsa_pc = (nsa_pc/100)^2 
replace rf_pc = (rf_pc/100)^2 
replace v_pc = (v_pc/100)^2 
replace hfv_pc = (hfv_pc/100)^2 
replace sp_pc = (sp_pc/100)^2 
replace nkp_pc = (nkp_pc/100)^2 

gen hhi = (dna_pc + nsa_pc + rf_pc + v_pc + hfv_pc + sp_pc + nkp_pc)*100
drop dna_pc nsa_pc rf_pc v_pc hfv_pc sp_pc nkp_pc

gen dna_pc = (votesdna/validv)*100                                              //generating percentgage of votes for each party
gen nsa_pc = (votesnsa/validv)*100
gen rf_pc = (votesrf/validv)*100
gen v_pc = (votesv/validv)*100
gen hfv_pc = (voteshfv/validv)*100
gen sp_pc = (votessp/validv)*100
gen nkp_pc = (votesnkp/validv)*100

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor24.dta", replace
		

*****************************************************
*****************************************************
**              CREATING NOR27.DTA                 **
*****************************************************
*****************************************************	
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor27") firstrow case(lower) clear

gen votesf = personalvotewomen + mailwomen
gen votesm = personalvotesmen + mailmen
drop personalvotesmen personalvotewomen personalvoteoverall mailmen mailwomen mailoverall
rename valgkrets districtPR                                                     
rename knr id
rename knavn munic_name
rename eligibleoverall elect                                                    //renaming vars to be consistent with all other data sets
rename eligiblewomen electf
rename eligiblemen electm
rename castedvotesoverall votes
rename approvedvotesoverall validv

replace votesoth = votesoth + votesvil+ votesrf                                 //adding  small parties (less than 1.5%) to `other' category"
generate voteshfv = votesh_fv + votesfv                                         //Hoyre and Frisinnede Venstre run in a national coalition in 1927
drop  votesh_fv  votesfv  votesvil votesrf 

gen dna_pc = (votesdna/validv)*100                                              //generating percentgage of votes for each party
gen nkp_pc = (votesnkp/validv)*100
gen v_pc = (votesv/validv)*100
gen hfv_pc = (voteshfv/validv)*100
gen sp_pc = (votessp/validv)*100

gen turnm_pc = (votesm/electm)*100                                              //generating turnout by sex 
gen turnf_pc = (votesf/electf)*100
gen turng = turnf_pc - turnm_pc                                                 // generating gender turnout gap
gen turn_pc = (votes/elect)*100
gen wsh = (votesf/(votesm+ votesf))*100                                          //generating women's share of voters                                                   

replace dna_pc = (dna_pc/100)^2                                                 //generating HHI
replace nkp_pc = (nkp_pc/100)^2 
replace v_pc = (v_pc/100)^2 
replace hfv_pc = (hfv_pc/100)^2 
replace sp_pc = (sp_pc/100)^2 
gen hhi = (dna_pc + nkp_pc  + v_pc + hfv_pc + sp_pc)*100
drop dna_pc nkp_pc  v_pc hfv_pc sp_pc

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor27.dta", replace


*****************************************************
*****************************************************
**              CREATING NOR2127.DTA               **
*****************************************************
*****************************************************	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta", replace
preserve
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor24.dta"	
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor27.dta"	
centile hhi , c( 0 10 20 30 40 50 60 70 80 90 100)  //generating centiles of HHI                  
ret li
gen hhi_c="10" if  hhi>=`r(c_1)' & hhi<=`r(c_2)'
replace hhi_c="20" if hhi>`r(c_2)' & hhi<=`r(c_3)'
replace hhi_c="30" if hhi>`r(c_3)' & hhi<=`r(c_4)'
replace hhi_c="40" if hhi>`r(c_4)' & hhi<=`r(c_5)'
replace hhi_c="50" if hhi>`r(c_5)' & hhi<=`r(c_6)'
replace hhi_c="60" if hhi>`r(c_6)' & hhi<=`r(c_7)'
replace hhi_c="70" if hhi>`r(c_7)' & hhi<=`r(c_8)'
replace hhi_c="80" if hhi>`r(c_8)' & hhi<=`r(c_9)'
replace hhi_c="90" if hhi>`r(c_9)' & hhi<=`r(c_10)'
replace hhi_c="100" if hhi>`r(c_10)' & hhi<=`r(c_11)'
gen hhi_10 = .
replace hhi_10 = 1 if hhi_c=="10"
replace hhi_10 = 2 if hhi_c=="20"
replace hhi_10 = 3 if hhi_c=="30"
replace hhi_10 = 4 if hhi_c=="40"
replace hhi_10 = 5 if hhi_c=="50"
replace hhi_10 = 6 if hhi_c=="60"
replace hhi_10 = 7 if hhi_c=="70"
replace hhi_10 = 8 if hhi_c=="80"
replace hhi_10 = 9 if hhi_c=="90"
replace hhi_10 = 10 if hhi_c=="100"
drop hhi_c
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor2127.dta", replace
restore


*****************************************************
*****************************************************
**          CREATING NOR2127_red.DTA               **
*****************************************************
*****************************************************	

import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("redist2127") firstrow case(lower) clear
rename knr id
drop year
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/redist2127.dta", replace

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta"
preserve
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor24.dta"	
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor27.dta"	
drop _merge
merge m:m id using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/redist2127.dta"   //merging  municipalities with changes in boundaries	
gen redist = 0                                                                                             //identifying municipalities with changes in boundaries
replace redist=1 if _merge==3
drop _merge
drop if redist==1
replace knr = id if year==1924
replace knr = id if year==1927
xtset knr year
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor2127_red.dta", replace
restore
	
	
*****************************************************
*****************************************************
**        CREATING NOR2127_distr.DTA               **
*****************************************************
*****************************************************	
*collapsing 2127 on district
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor2127.dta", replace

preserve
collapse (sum) votesdna validv adultwom15 adultman15 marwom15 intelwom intelmen factwom factmen  electm electf elect votes votesf votesm urban    , by(districtPR year) 
gen turnm_pc = (votesm/electm)*100                                              //generating turnout by sex 
gen turnf_pc = (votesf/electf)*100
gen turng = turnf_pc-turnm_pc
gen turn_pc = (votes/elect)*100
gen fact20 = ((factmen + factwom) / (adultman15+adultwom15))*100    //generating percentage industrial workers
gen intel20 = ((intelmen + intelwom) / (adultman15+adultwom15))*100 //generating percentage intellectual workers
gen factw20 = (factwom / adultman15)*100    //generating percentage industrial women workers 
gen intelw20 = (intelwom / adultwom15)*100 //generating percentage intellectual women workers
gen marw20 = (marwom15 / adultwom15)*100   
gen dna_pc = (votesdna/validv)*100                                              //generating percentgage of votes for each party
gen urban01 = urban
replace urban01 =1 if urban>1
xtset districtPR year
replace fact20 = L3.fact20 if fact20==.
replace factw20 = L3.factw20 if factw20==.
replace intelw20 = L3.intelw20 if intelw20==.
replace intel20 = L3.intel20 if intel20==.
replace marw20 = L3.marw20 if marw20==.
gen el_rat = electm/electf
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor2127_district.dta", replace
restore

*adding  margin, as in cox, fiva and smith (2016, 2020)
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor2127_distr_coxfivasmith") firstrow case(lower) clear
rename  pr_district districtPR
replace margin = margin*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor2127_distr_coxfivasmith.dta", replace

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor2127_district.dta"
merge 1:1 districtPR year using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor2127_distr_coxfivasmith.dta",

  Result                           # of obs.
    -----------------------------------------
    not matched                            22
        from master                        22  (_merge==1) //7 PR districts dropped by Cox, Fiva and Smith (2016, 2020)
        from using                          0  (_merge==2)

    matched                                66  (_merge==3)
    -----------------------------------------

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor2127_district.dta", replace
	
	
*****************************************************
*****************************************************
**         CREATING NOR2118_munic.DTA              **
*****************************************************
*****************************************************	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta", replace
preserve
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_munic.dta"

xtset knr year
gen dgg = turng-L3.turng
gen dturnm = turnm_pc -L3.turnm_pc
gen dturnf = turnf_pc -L3.turnf_pc	
gen Lturnm_pc = L3.turnm_pc	

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor2118_munic.dta", replace
restore


*****************************************************
*****************************************************
**           CREATING NOROVERT.DTA                 **
*****************************************************
*****************************************************	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09.dta", replace
preserve
collapse (sum) votesf votesm votes electf electm elect (mean) year                        //generating turnout at national level in 1909
gen turnm_pc = (votesm/electm)*100                                                      
gen turnf_pc = (votesf/electf)*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert.dta", replace
restore	
	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor12.dta", replace
preserve
collapse (sum) votesf votesm votes electf electm elect (mean) year                        //generating turnout at national level in 1912
gen turnm_pc = (votesm/electm)*100                                                      
gen turnf_pc = (votesf/electf)*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert12.dta", replace
restore		
		
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15.dta", replace
preserve
collapse (sum) votesf votesm votes electf electm elect (mean) year                        //generating turnout at national level in 1915
gen turnm_pc = (votesm/electm)*100                                                      
gen turnf_pc = (votesf/electf)*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert15.dta", replace
restore		
	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18.dta", replace
preserve
collapse (sum) votesf votesm votes electf electm elect (mean) year                        //generating turnout at national level in 1918
gen turnm_pc = (votesm/electm)*100                                                      
gen turnf_pc = (votesf/electf)*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert18.dta", replace
restore			
	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor21.dta", replace
preserve
collapse (sum) votesf votesm votes electf electm elect (mean) year                        //generating turnout at national level in 1921
gen turnm_pc = (votesm/electm)*100                                                      
gen turnf_pc = (votesf/electf)*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert21.dta", replace
restore			
	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor24.dta", replace
preserve
collapse (sum) votesf votesm votes electf electm elect (mean) year                        //generating turnout at national level in 1921
gen turnm_pc = (votesm/electm)*100                                                      
gen turnf_pc = (votesf/electf)*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert24.dta", replace
restore

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor27.dta", replace
preserve
collapse (sum) votesf votesm votes electf electm elect (mean) year                        //generating turnout at national level in 1921
gen turnm_pc = (votesm/electm)*100                                                      
gen turnf_pc = (votesf/electf)*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert27.dta", replace
restore		
	
use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert.dta"
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert12.dta"
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert15.dta"
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert18.dta"
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert21.dta"
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert24.dta"
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert27.dta"
gen gg = round(turnm-turnf, 0.1)
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norovert.dta", replace
	
		
*****************************************************
*****************************************************
**          CREATING NOR_coxfivasmith.DTA          **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor_coxfivasmith") firstrow case(lower) clear

** Replication files Cox, Smith and Fiva (2016)
/* MAKING FINAL ROUND VARIABLES */
gen turnout_mun_final=turnout_mun
replace turnout_mun_final=fut_turnout if fut_turnout!=.   /* replacing with second round turnout, if that exists */
gen totalvotes_final=totalvotes_mun
replace totalvotes_final=fut_totalvotes_mun if fut_totalvotes_mun!=.   /* replacing with second round turnout, if that exists */

/* MAKING FINAL ROUND MARGIN */
gen marginf = margin
replace marginf=fut_margin if fut_margin!=.   /* replacing with second round margin, if that exists */

/* KEEPING ONLY ONE ROUND OF MUNICIP ELECTIONS AND MERGING PR PERIOD */
keep if round==1

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor_coxfivasmith.dta", replace

** MMC: merging sex-separated data 1918 with nor_coxsmithfiva
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor18_munic") firstrow case(lower) clear
gen year=1918
gen eligible1men = eligible1overall-eligible1women                                                    //generating male electorate and votes
gen castedvotes1men = castedvotes1overall-castedvotes1women
gen castedvotes2men = castedvotes2overall-castedvotes2women
keep knr year  eligible1women eligible1men castedvotes1women castedvotes1men castedvotes2women castedvotes2men 
foreach v of varlist eligible1women eligible1men castedvotes1women castedvotes1men castedvotes2women castedvotes2men {
replace `v' = . if `v'==9999999
}
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_mcox.dta", replace


use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor_coxfivasmith.dta", replace
merge 1:1 knr year using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor18_mcox.dta"
	
 Result                           # of obs.
    -----------------------------------------
    not matched                         2,054
        from master                     2,047  (_merge==1) 
        from using                          7  (_merge==2)                      //7 split municipalities excluded by Cox et al 2016

    matched                               689  (_merge==3)
    -----------------------------------------

drop if _merge==2
drop _merge

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor_coxfivasmith.dta", replace

** Replication files Cox, Fiva and Smith 2016 - adding 1921
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor_coxfivasmith21") firstrow case(lower) clear
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor_coxfivasmith21.dta", replace

use "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor_coxfivasmith.dta", replace
append using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor_coxfivasmith21.dta"
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor_coxfivasmith.dta", replace

** Replication files Cox, Smith and Fiva 92016) - pre-collapse data prep
/* CREATING DISTRICT LEVEL VARIABLES */
egen SMD_district=max(valgkrets), by(knr)   /* valgkrets is between 101 and 2002 pre-reform and between 1 and 31 post-reform */
egen PR_district=min(valgkrets), by(knr)   /* valgkrets is between 101 and 2002 pre-reform and between 1 and 31 post-reform */

** oslo (5), drammen (2), kristiansand (2) stavanger (2), bergen (4), trondheim (4)
** ... consist of multiple districts within a municipality. We do not have the possibility to get post-reform data on that level
** ... so we lose these 19 districts. Leaving us with 126-19-12=95 districts.
** some municipalities are split after reform , need to manually find their SMD_district 

sort knr year
li knr SMD_district if SMD_district<100

replace SMD_district=101 if knr==126 /* 126 mysen was part of 125 eidsberg up to 1920 */
replace SMD_district=203 if knr==213 /* 213 ski was part of 212 krÂkstad up to 1931 */
replace SMD_district=203 if knr==222 /* 222 nordre h¯land was part of 221 h¯land */
replace SMD_district=203 if knr==225
replace SMD_district=203 if knr==228
*replace SMD_district=XXX if knr==301  /* oslo */
replace SMD_district=406 if knr==435
*replace SMD_district=XXX if knr==602 /* drammen */
replace SMD_district=902 if knr==917
*replace SMD_district=XXX if knr==1001
*replace SMD_district=XXX if knr==1103
replace SMD_district=1103 if knr==1125
replace SMD_district=1103 if knr==1127
replace SMD_district=1103 if knr==1144
replace SMD_district=1104 if knr==1151
replace SMD_district=1105 if knr==1156
replace SMD_district=1105 if knr==1158
replace SMD_district=1204 if knr==1256
replace SMD_district=1204 if knr==1260
replace SMD_district=1204 if knr==1262
*replace SMD_district=XXX if knr==1301
replace SMD_district=1404 if knr==1435
replace SMD_district=1404 if knr==1436
replace SMD_district=1502 if knr==1518
replace SMD_district=1504 if knr==1547
*replace SMD_district= if knr==1601
replace SMD_district=1602 if knr==1611
replace SMD_district=1602 if knr==1613
replace SMD_district=1603 if knr==1637
replace SMD_district=1603 if knr==1639
replace SMD_district=1604 if knr==1641
replace SMD_district=1604 if knr==1642
replace SMD_district=1604 if knr==1643
*replace SMD_district= if knr==1701
replace SMD_district=1703 if knr==1739
replace SMD_district=1703 if knr==1740
replace SMD_district=1703 if knr==1741
replace SMD_district=1801 if knr==1801
replace SMD_district=1802 if knr==1803
replace SMD_district=1802 if knr==1823
replace SMD_district=1802 if knr==1825
replace SMD_district=1802 if knr==1829
replace SMD_district=1802 if knr==1831
replace SMD_district=1804 if knr==1854
replace SMD_district=1805 if knr==1856
replace SMD_district=1805 if knr==1861
replace SMD_district=1805 if knr==1863
replace SMD_district=1806 if knr==1869
replace SMD_district=1806 if knr==1871
replace SMD_district=1806 if knr==1873
replace SMD_district=1901 if knr==1912
replace SMD_district=1901 if knr==1913
replace SMD_district=1902 if knr==1916
replace SMD_district=1902 if knr==1918
replace SMD_district=1902 if knr==1919
replace SMD_district=1903 if knr==1923
replace SMD_district=1904 if knr==1939
replace SMD_district=1904 if knr==1940

drop if SMD_district<100  /* these are the multiple-districts municipalities post-reform */
drop if SMD_district>3000  /* these are the multiple-districts municipalities pre-reform */

drop if SMD_district==690
drop if SMD_district==892
drop if SMD_district==1001

** MMC indicating additional dropped districts in Cox, Smith and Fiva (2016)
gen excl0918 = 0
replace excl0918 = 1 if SMD_district==400
replace excl0918 = 1 if SMD_district==490
replace excl0918 = 1 if SMD_district==500
replace excl0918 = 1 if SMD_district==1501
replace excl0918 = 1 if SMD_district==1502
replace excl0918 = 1 if SMD_district==1503
replace excl0918 = 1 if SMD_district==1504
replace excl0918 = 1 if SMD_district==1505
replace excl0918 = 1 if SMD_district==1506
replace excl0918 = 1 if SMD_district==1901
replace excl0918 = 1 if SMD_district==1902
replace excl0918 = 1 if SMD_district==1903
replace excl0918 = 1 if SMD_district==1904
replace excl0918 = 1 if SMD_district==1280

** replication files Cox, Smith and Gova 2016 - dropping additional districts 

/* Lillehammer, Hamar, Gj¯vik og Kongsvinger delt i to */
*drop if valgkrets==400 
*drop if valgkrets==490
*drop if valgkrets==500

/* extra district was added in Romsdal, and then municipalities appear to be reshuffled from 1915 to 1918 */
*drop if valgkrets==1501
*drop if valgkrets==1502
*drop if valgkrets==1503
*drop if valgkrets==1504
*drop if valgkrets==1505
*drop if valgkrets==1506

/* extra district was added in Troms, and then municipalities appear to be slightly reshuffled from 1915 to 1918 */
*drop if valgkrets==1901
*drop if valgkrets==1902
*drop if valgkrets==1903
*drop if valgkrets==1904

*drop if knr==1280 /* aarstad part of Bergen from 1.7.1915 */

** replication files Cox, Smith and Gova 2016 - generating vote totals
replace eligibleoverall=eligible1overall if year<1920   /* eligibleoverall is a variable existing only for PR, eligible1overall is the first round elig in SMD */
replace approvedvotesoverall=totalvotes_final if year<1920 /* approvedvotesoverall is a variable existing only for PR, totalvotes_final is the final round no. votes in SMD */

** MMC: generating sex-separated vote totals
replace eligiblemen=eligible1men if year<1920   
replace eligiblewomen=eligible1women if year<1920   
gen castedvotesmen = castedvotes1men                                                         
replace castedvotesmen = castedvotes2men if castedvotes2men!=0
gen castedvoteswomen = castedvotes1women                                                         
replace castedvoteswomen = castedvotes2women if castedvotes2women!=0
replace castedvotesmen = personalvotesmen + mailmen if year==1921
replace castedvoteswomen = personalvotewomen + mailwomen if year==1921

** MMC: merging 1920 census
merge m:1 knr using "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/norcens20.dta" 	

  Result                           # of obs.
    -----------------------------------------
    not matched                            21
        from master                         2  (_merge==1)                      //knr 1280 not in census 1920
        from using                         19  (_merge==2)                      //dropped 19 multi-district municipalities

    matched                             3,298  (_merge==3)
    -----------------------------------------

** MMC: collapsing at pre-reform district with census controls 
collapse (mean) PR_district excl0918  margin marginf (sum) eligibleoverall eligiblemen eligiblewomen  approvedvotesoverall castedvoteswomen castedvotesmen totalvotes_mun_v_a totalvotes_mun_h_fv totalvotes_mun_s totalvotes_mun_oth   city adultwom15 adultman15 marwom15 intelwom intelmen factwom factmen      , by(SMD_district year) 

** replication files Cox, Smith and Fiva 2016 - generating turnout and other variables
xtset SMD_district year
gen xx=1
bysort SMD_district: egen zz=total(xx)
keep if zz==5 //MMC changed to 5
drop xx zz

gen turnout=approvedvotesoverall/eligibleoverall

bysort SMD_district: egen SMD_margin_pre=mean(margin)  /* AVG. DIFFERENCE BETWEEN WINNER AND RUNNER UP IN FIRST ROUND IN SMD PERIOD */

sort SMD_district year
gen ch_turnout=turnout-l3.turnout

gen Margin=SMD_margin_pre

** MMC: generating sex-separated turnout and additional variables
gen turnoutm=castedvotesmen/eligiblemen
gen turnoutw=castedvoteswomen/eligiblewomen
gen ch_turnoutm=turnoutm-l3.turnoutm
gen ch_turnoutw=turnoutw-l3.turnoutw
gen turng = turnoutw-turnoutm
gen ch_turnoutg = turng-l3.turng
gen turnoutwsh = (castedvoteswomen/(castedvotesmen + castedvoteswomen))                                          //generating women's share of voters                                                   
gen ch_turnoutwsh = turnoutwsh-l3.turnoutwsh
gen turnoutm18 = turnoutm if year==1918
replace turnoutm18 = L3.turnoutm if year==1921

gen margin18 = L3.margin if year==1921
gen margin18f = L3.marginf if year==1921

gen el_rat = eligiblemen/eligiblewomen
gen fact20 = ((factmen + factwom) / (adultman15+adultwom15))*100    //generating percentage industrial workers
gen intel20 = ((intelmen + intelwom) / (adultman15+adultwom15))*100 //generating percentage intellectual workers
gen factw20 = (factwom / adultman15)*100    //generating percentage industrial women workers 
gen intelw20 = (intelwom / adultwom15)*100 //generating percentage intellectual women workers
gen marw20 = (marwom15 / adultwom15)*100                            //generating percentage married women over 15 years
gen urban = L3.city if year==1921
replace urban = 1 if urban==2
replace urban = 1 if urban==3

** MMC generating soc_lead 1918 
 gen va_pc = totalvotes_mun_v_a /approvedvotesoverall
 gen hfv_pc = totalvotes_mun_h_fv / approvedvotesoverall
 gen s_pc = totalvotes_mun_s /approvedvotesoverall 
 gen oth_pc = totalvotes_mun_oth /approvedvotesoverall
 
egen max1 = rowmax(s_pc va_pc oth_pc hfv_pc)                               //generating indicators for a frontrunner and runner up candidate for figure A6
rowsort s_pc va_pc oth_pc hfv_pc, gen(p1-p4) descending        
foreach var of varlist p1 p2 p3 p4  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4)

gen ConLib1 = .                                                                    
replace ConLib1 = 1 if max1==hfv_pc & max2==va_pc
replace ConLib1 = 1 if max2==hfv_pc & max1==va_pc

gen ConSoc1 = .
replace ConSoc1 = 1 if max1==hfv_pc & max2==s_pc
replace ConSoc1 = 1 if max2==hfv_pc & max1==s_pc

gen LibSoc1 = .
replace LibSoc1 = 1 if max1==va_pc & max2==s_pc
replace LibSoc1 = 1 if max2==va_pc & max1==s_pc

drop  max1 p1 p2 p3 p4 max2 

gen soccont = 0                                                                      //generting indicator if districts where socialist candidate is a contender (first or second) in the first round
replace soccont = 1 if ConSoc1==1
replace soccont = 1 if LibSoc1==1

replace soccont = L3.soccont if year==1921

** MMC generating variables for marginal effects
sum ch_turnoutm, det, if year==1921
gen ch_turnoutm03 = 3 if ch_turnoutm>0.19 //big increase, larger than 90th percentile
replace ch_turnoutm03 = 2 if ch_turnoutm<0.19 & ch_turnoutm>0 //small increase 0-0.19
replace ch_turnoutm03 = 1 if ch_turnoutm<0 //decrease
label define ch_turn03 1 "Decrease" 2 "Small Increase" 3 "Large Increase"
label value ch_turnoutm03 ch_turn03 

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor_coxfivasmith.dta", replace


*****************************************************
*****************************************************
**              CREATING NOR09_AD.DTA              **
*****************************************************
*****************************************************	
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor09_ad") firstrow case(lower) clear
drop if districtid>3000                         //dropping multi-district municipalities
gen ads_gr = 0
replace ads_gr = 1 if ads==1                                 
label define ads_gr 0 "No Ads" 1 "At Least One Ad"
label value ads_gr ads_gr
gen ads_grw = 0
replace ads_grw = 1 if ads_women==1                                 
label value ads_grw ads_gr
gen margin = districtmargin1
replace margin = districtmargin2 if districtmargin2!=.
replace margin = margin*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor09_ad.dta", replace

	
*****************************************************
*****************************************************
**              CREATING NOR15_AD.DTA              **
*****************************************************
*****************************************************	
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nor15_ad") firstrow case(lower) clear
drop if districtid>3000                         //dropping multi-district municipalities
gen ads_gr = 0
replace ads_gr = 1 if ads==1                                 
label define ads_gr 0 "No Ads" 1 "At Least One Ad"
label value ads_gr ads_gr
gen ads_grw = 0
replace ads_grw = 1 if ads_women==1                                 
label value ads_grw ads_gr
gen margin = districtmargin1
replace margin = districtmargin2 if districtmargin2!=.
replace margin = margin*100
save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nor15_ad.dta", replace

	
*****************************************************
*****************************************************
**                CREATING NZ.DTA                 **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("nz") firstrow case(lower) clear

label var district "electoral district"
label var elect "total electorate/registered"
label var electm "male electorate"
label var electf "female electorate"
label var votes "total votes cast"
label var votesm "male total votes cast"
label var votesf "female total votes cast"
label var validv "total valid votes"
label var candidate1_name "name of candidate 1" 
label var candidate2_name "name of candidate 2" 
label var candidate3_name "name of candidate 3" 
label var candidate4_name "name of candidate 4" 
label var candidate5_name "name of candidate 5" 
label var cand1v "votes for candidate 1" 
label var cand2v "votes for candidate 2" 
label var cand3v "votes for candidate 3" 
label var cand4v "votes for candidate 4" 
label var cand5v "votes for candidate 5" 

gen cand1_pc = (cand1v/validv)*100                                                //generating percentgage of votes for each candidate
gen cand2_pc = (cand2v/validv)*100
gen cand3_pc = (cand3v/validv)*100
gen cand4_pc = (cand4v/validv)*100
gen cand5_pc = (cand5v/validv)*100

gen turnm_pc = (votesm/electm)*100                                                //generating turnout by sex 
gen turnf_pc = (votesf/electf)*100
gen turn_pc = (votes/elect)*100
gen turng = turnf_pc-turnm_pc

drop if cand1_pc >100 & cand1_pc<.                                                //dropping one district where candidate votes exceed validv

replace cand1_pc = 0 if cand1_pc==.                                                  
replace cand2_pc = 0 if cand2_pc==.
replace cand3_pc = 0 if cand3_pc==.
replace cand4_pc = 0 if cand4_pc==.
replace cand5_pc = 0 if cand5_pc==.

egen max1 = rowmax(cand1_pc cand2_pc cand3_pc cand4_pc cand5_pc)                   //generating margin between top two candidates
rowsort cand1_pc cand2_pc cand3_pc cand4_pc cand5_pc, gen(p1-p5) descending        
foreach var of varlist p1 p2 p3 p4 p5  {
 replace `var' = . if `var'==max1
}
egen max2 = rowmax(p1 p2 p3 p4 p5)
gen margin = max1-max2
drop  max1 p1 p2 p3 p4 p5 max2 cand1_pc cand2_pc cand3_pc cand4_pc cand5_pc

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/nz.dta", replace

	
*****************************************************
*****************************************************
**                CREATING SWE.DTA                 **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("swe") firstrow case(lower) clear

label var id "geokod"
label var county "län"
label var district "härad"
label var munic_name "municipality name"
label var redistr20 "redistricting 1920"
label var redistr21 "redistricting 1921"
label var elect "total electorate/eligibles"
label var electm "male electorate"
label var electf "female electorate"
label var votes "total votes cast"
label var votesm "male total votes cast"
label var votesf "female total votes cast"
label var validv "total valid votes"
label var invalidv "total invalid votes"
label var conv "total conservative votes (Högern)" 
label var libv "total liberal votes (Liberala)" 
label var socv "total socialist votes (Socialdemocrater)" 
label var agrv "total agrarian votes (Bondeförbundet)" 
label var agr2v "total agrarian votes (Jordbrukarnas Riksförbund)" 
label var comv "total communist votes (Vänster Socialister/och Kommunister)" 

encode county, gen(county_enc)

gen con_pc = (conv/validv)*100                                                   //generating percentgage of votes for each party
gen soc_pc = (socv/validv)*100
gen lib_pc = (libv/validv)*100
gen agr_pc = (agrv/validv)*100
gen agr2_pc = (agr2v/validv)*100
gen com_pc = (comv/validv)*100

gen turnm_pc = (votesm/electm)*100                                                //generating turnout by sex in 1921
gen turnf_pc = (votesf/electf)*100
gen turng = turnf_pc - turnm_pc                                                    //generating gender turnot gap in 1921
gen turn_pc = (votes/elect)*100

drop if turnm_pc>100 & turnm_pc<.                                                 //dropping 2 municipalities with male turnout over 100% in 1921

drop if ((votesm + votesf)-votes)<0                                               //dropping 16 municipalities with higher sex-separated vote total than total votes cast in 1921
drop if ((votesm + votesf)-votes)>0  & ((votesm + votesf)-votes)<.                //dropping 8 municipalities with lower sex-separated vote total than total votes cast in 1921

gen incpty20 = con_pc + soc_pc + lib_pc + agr_pc + agr2_pc + com_pc if year==1920 //identifying municipalities with inconsistent party shares in 1920
gen incpty21 = con_pc + soc_pc + lib_pc + agr_pc  + com_pc if year==1921          //identifying municipalities with inconsistent party shares in 1921 
drop if incpty21>100 & incpty21<.                                                 //dropping 11 municipalities with party totals over 100% in 1921
drop if incpty20>100 & incpty20<.                                                 //dropping 26 municipalities with party totals over 100% in 1920 
drop incpty20 incpty21

replace soc_pc = 0 if soc_pc==.                                                   //preparing calculation of HHI
replace lib_pc = 0 if lib_pc==.
replace con_pc = 0 if con_pc==.
replace com_pc = 0 if com_pc==.
replace agr_pc = 0 if agr_pc==.
replace agr2_pc = 0 if agr2_pc==.
replace soc_pc = (soc_pc/100)^2                                                   //generating HHI
replace lib_pc = (lib_pc/100)^2 
replace con_pc = (con_pc/100)^2 
replace com_pc = (com_pc/100)^2 
replace agr_pc = (agr_pc/100)^2 
replace agr2_pc = (agr2_pc/100)^2 
gen hhi = (soc_pc + lib_pc + con_pc + com_pc + agr_pc + agr2_pc)*100
xtset  id year
gen hhiL = L.hhi                                                                   //generating t-1 (pre-suffrage) HHI
replace hhiL=. if hhiL==0                                                          //2 municipalities with no 1920 entry into missing
drop if hhi==0                                                                     //dropping 105 municipalities with no election data
drop con_pc soc_pc lib_pc agr_pc agr2_pc com_pc

centile hhi , c( 0 10 20 30 40 50 60 70 80 90 100)                    
ret li
gen hhi_c="10" if  hhi>=`r(c_1)' & hhi<=`r(c_2)'
replace hhi_c="20" if hhi>`r(c_2)' & hhi<=`r(c_3)'
replace hhi_c="30" if hhi>`r(c_3)' & hhi<=`r(c_4)'
replace hhi_c="40" if hhi>`r(c_4)' & hhi<=`r(c_5)'
replace hhi_c="50" if hhi>`r(c_5)' & hhi<=`r(c_6)'
replace hhi_c="60" if hhi>`r(c_6)' & hhi<=`r(c_7)'
replace hhi_c="70" if hhi>`r(c_7)' & hhi<=`r(c_8)'
replace hhi_c="80" if hhi>`r(c_8)' & hhi<=`r(c_9)'
replace hhi_c="90" if hhi>`r(c_9)' & hhi<=`r(c_10)'
replace hhi_c="100" if hhi>`r(c_10)' & hhi<=`r(c_11)'
gen hhi_10 = .
replace hhi_10 = 1 if hhi_c=="10"
replace hhi_10 = 2 if hhi_c=="20"
replace hhi_10 = 3 if hhi_c=="30"
replace hhi_10 = 4 if hhi_c=="40"
replace hhi_10 = 5 if hhi_c=="50"
replace hhi_10 = 6 if hhi_c=="60"
replace hhi_10 = 7 if hhi_c=="70"
replace hhi_10 = 8 if hhi_c=="80"
replace hhi_10 = 9 if hhi_c=="90"
replace hhi_10 = 10 if hhi_c=="100"
drop hhi_c

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/swe.dta", replace


*****************************************************
*****************************************************
**                CREATING AUS.DTA                 **
*****************************************************
*****************************************************
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("aus") firstrow case(lower) clear

gen id = _n
label var id "munic id"
label var state "Land"
label var const "Vahlkreis"
label var munic_name "municipality name (Ortsgemeinde)"
label var elect "total electorate/eligibles"
label var electm "male electorate"
label var electf "female electorate"
label var votes "total votes cast"
label var votesm "male total votes cast"
label var votesf "female total votes cast"
label var validv "total valid votes"
label var invalidv "total invalid votes"
label var unityv "total unity coalition votes (Einheitsliste)" 
label var agrv "total agrarian votes (Landbund)" 
label var socv "total socialist votes (Sozialdemokratische)" 
label var otherv "total other votes" 

encode const, gen(const_enc)

gen unity_pc = (unityv/validv)*100                                                //generating percentgage of votes for each party
gen soc_pc = (socv/validv)*100
gen agr_pc = (agrv/validv)*100
gen other_pc = (otherv/validv)*100
replace soc_pc = 0 if soc_pc==.                                                  
replace unity_pc = 0 if unity_pc==.
replace agr_pc = 0 if agr_pc==.
replace other_pc = 0 if other_pc==.

gen turnm_pc = (votesm/electm)*100                                                //generating turnout by sex 
gen turnf_pc = (votesf/electf)*100
gen turng = turnf_pc - turnm_pc                                                    //generating gender turnot gap 
gen turn_pc = (votes/elect)*100

drop if turnm_pc>100 & turnm_pc<.                                                 //dropping 167 municipalities with male turnout over 100% 
drop if turnf_pc>100 & turnf_pc<.                                                 //dropping 29 municipalities with female turnout over 100% 
drop if turnm_pc<0                                                                //dropping 3 municipalities with male turnout below 0% 
drop if ((electm + electf)-elect)<0                                               //dropping 1 municipalities with higher sex-separated vote total than total votes cast 
drop if ((electm + electf)-elect)>0  & ((electm + electf)-elect)<.                //dropping 10 municipalities with lower sex-separated vote total than total votes cast
drop if votes<validv                                                              //dropping 19 municipalities which return higher valid votes cast than votes cast

gen incpty27 = unity_pc + soc_pc + agr_pc +other_pc                               //identifying municipalities with inconsistent party shares 
drop if incpty27>100 & incpty27<.                                                 //dropping 152 municipalities with party totals over 100% 
drop incpty27

replace soc_pc = (soc_pc/100)^2                                                   //generating HHI
replace unity_pc = (unity_pc/100)^2 
replace agr_pc = (agr_pc/100)^2 
gen hhi = (soc_pc + unity_pc + agr_pc)*100 

drop unity_pc soc_pc agr_pc

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/aus.dta", replace


*****************************************************
*****************************************************
**              CREATING TINGSTEN.DTA              **
*****************************************************
*****************************************************	
import excel "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/data_raw.xlsx", sheet("tingsten") firstrow case(lower) clear

gen gg = vtmen-vtwom             //generating percentgage point difference between women's and men's turnout (gender gap)	
egen country_ranked_gg = rank(gg)
labmask country_ranked_gg, val(country_lab)

save "/Users/Mona/Dropbox/VT AGGREG/Submission/replication/build data sets/tingsten.dta", replace




